from PySide6.QtGui import QIcon
from PySide6.QtWidgets import *
from qfluentwidgets import FlowLayout
from qfluentwidgets.window.stacked_widget import StackedWidget

from components import AppCard
from .ppt_view import PptToPdfView, PPtToImgView, PPtReplaceView
from resource import OfficeIcon


class PPtHomeFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = FlowLayout(self, needAni=True)

        self.to_pdf = AppCard(OfficeIcon.PDF3, 'PPT To PDF', 'PPT转PDF')
        self.to_img = AppCard(OfficeIcon.IMG, 'PPT To IMG', 'PPT转图片（单张/多张）')
        self.ppt_replace = AppCard(OfficeIcon.DATA1, 'PPT 数据替换', '替换PPT中的指定数据')

        self.layout.addWidget(self.to_pdf)
        self.layout.addWidget(self.to_img)
        self.layout.addWidget(self.ppt_replace)


class PPtInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("PPtInterface")
        self.stackWidget = QStackedWidget(self)

        self.initUI()
        self.initLayout()
        self.initButtonEvent()

    def initUI(self):
        self.homeFrame = PPtHomeFrame()
        self.to_pdf = PptToPdfView()
        self.to_img = PPtToImgView()
        self.pptReplace = PPtReplaceView()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.to_pdf)
        self.stackWidget.addWidget(self.to_img)
        self.stackWidget.addWidget(self.pptReplace)

        self.stackWidget.setCurrentIndex(0)

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 40, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def initButtonEvent(self):
        self.homeFrame.to_pdf.clicked.connect(lambda: self.switchTo(self.to_pdf))
        self.homeFrame.to_img.clicked.connect(lambda: self.switchTo(self.to_img))
        self.homeFrame.ppt_replace.clicked.connect(lambda: self.switchTo(self.pptReplace))

        self.to_pdf.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.to_img.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.pptReplace.returned.connect(lambda: self.switchTo(self.homeFrame))

    def switchTo(self, widget):
        self.stackWidget.setCurrentWidget(widget)
